using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Threading;

using SqlWebAdmin.CSharp;

public partial class Tables : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
            lblError.Visible = false;
            dgTables.Visible = false;

            string databaseName = Request.QueryString["database"];


			Server smoServer = SmoHelper.GetServer();
            try
            {
                Database smoDatabase = smoServer.Databases[databaseName];
                if (smoDatabase != null)
                {

                    Microsoft.SqlServer.Management.Smo.Table smoTempTable = new Microsoft.SqlServer.Management.Smo.Table();
                    smoServer.SetDefaultInitFields(smoTempTable.GetType(), "IsSystemObject", "CreateDate");

					smoDatabase.Refresh();
                    TableCollection smoTables = smoDatabase.Tables;

					List<Microsoft.SqlServer.Management.Smo.Table> tables = new List<Microsoft.SqlServer.Management.Smo.Table>();

					if (ddlFilter.SelectedValue.ToLower() == "show system tables only")
						tables = SmoHelper.FilterSystemObjects(smoDatabase.Tables, true);
					else if (ddlFilter.SelectedValue.ToLower() == "show user tables only")
						tables = SmoHelper.FilterSystemObjects(smoDatabase.Tables, false);

				//	tables[0].Cre
                    if (tables.Count > 0)
                    {
                                           
                        dgTables.DataSource = tables;
                        dgTables.DataBind();

                        dgTables.Visible = true;
                    }
                    else
                    {
                        lblError.Visible = true;
                    }
                }
                else
                {
                    Response.Redirect(String.Format("error.aspx?error={0}", Globals.databaseNotFoundErrorNumber), false);
                }

            }
            catch (SmoException smoEx)
            {
                lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(smoEx));
                lblError.Visible = true;
            }
            catch (System.Exception ex)
            {
                lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(ex));
                lblError.Visible = true;
            }
        
    }

    protected void btnAddNewTable_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Administration/CreateEditTable.aspx?database=" + HttpUtility.UrlEncode(Request.QueryString["database"]), false);
    }

    protected void ddlFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
    }

    protected void cmdFilter_Click(object sender, EventArgs e)
    {
    }

    protected void dgTables_ItemDataBound(object sender, DataGridItemEventArgs e)
    {

    }

    protected void dgTables_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }

    
}
